﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Custom.Controls
{
    public class SplashScreenManager
    {
        public static Form LoadingFrm;

        /// <summary>
        /// 显示加载窗体
        /// </summary>
        public static void ShowLoading(Form parentForm = null)
        {
            if (LoadingFrm == null)
            {
                LoadingFrm = new WaitScreenFrm(parentForm);
            }

            MethodInvoker invoker = new MethodInvoker(ShowWaitForm);
            invoker.BeginInvoke(null, null);

            while (!LoadingFrm.IsHandleCreated)
            {
            }

            // 把显示窗体放到最前面
            LoadingFrm.Invoke(new MethodInvoker(() =>
            {
                LoadingFrm.BringToFront();//放在前端显示
                LoadingFrm.Activate(); //当前窗体是LoadingForm
            }));

        }

        /// <summary>
        /// 关闭loading窗体
        /// </summary>
        public static void CloseWaitForm(Form parentFrom)
        {
            if (LoadingFrm == null || !LoadingFrm.IsHandleCreated)
            {
                return;
            }
            LoadingFrm.FormClosing += (sender, e) =>
            {
                // 重新激活下主窗体，要不然主窗体不会显示到桌面最前端（网上说这是windows的bug）
                LoadingFrm.Invoke(new Action(() =>
                {
                    if (parentFrom != null)
                    {
                        parentFrom.BringToFront();
                        parentFrom.Activate();
                    }
                }));
            };

            int err_count = 0;
            try
            {
                LoadingFrm.Invoke(new MethodInvoker(() =>
                {
                    LoadingFrm.Close();
                    LoadingFrm.Dispose();
                    LoadingFrm = null;
                }));
            }
            catch (Exception ex)
            {
                //防止未初始化,重复去close,直到OK
                bool isOK = false;
                err_count++;
                while (!isOK && err_count < 20)
                {
                    try
                    {
                        isOK = true;
                        LoadingFrm.Invoke(new MethodInvoker(() =>
                        {
                            LoadingFrm.Close();
                            LoadingFrm.Dispose();
                            LoadingFrm = null;
                        }));
                    }
                    catch (Exception)
                    {
                        isOK = false;
                        err_count++;
                    }
                }
            }
            finally
            {

            }
        }

        /// <summary>
        /// 显示等待窗体，并放到最前端
        /// </summary>
        private static void ShowWaitForm()
        {
            LoadingFrm.BringToFront();//放在前端显示
            LoadingFrm.Activate(); //当前窗体是LoadingForm
            LoadingFrm.ShowDialog();
        }
    }
}
